package com.share.pattern.singleton;

import java.util.Scanner;

/**
 * 
 * @author Johnny Tam 单例模式
 */
public class SingletonDemo {

	public static void main(String[] args) {

		// 1) Singleton实例
		Singleton s1 = Singleton.getInstance();
		Singleton s2 = Singleton.getInstance();

		if (s1 == s2) {
			System.out.println("两个对象是相同的");
		}
		
		//Demo compare
//		Demo demo1 = new Demo();
//		Demo demo2 = new Demo();
//		
//		if (demo1 == demo2) {
//			System.out.println("两个Demo对象是相同的");
//		}
		
		//2) 多线程时的单例
		//多线程同时访问，有可能产生多个实例， 如何避免？？
		Singleton2 s3 = Singleton2.getInstance();
		Singleton2 s4 = Singleton2.getInstance();
		
		if (s3 == s4) {
			System.out.println("两个Singleton2对象是相同的");
		}
		
		//3) 静态内部类
		Singleton3 s5 = Singleton3.getInstance();
		Singleton3 s6 = Singleton3.getInstance();
		
		if (s5 == s6) {
			System.out.println("两个Singleton3对象是相同的");
		}
				
	}

	

}

class Demo {
	private int number;
	Demo() {		
	}
}
